package com.example.oasystem.controller.attend;

import com.example.oasystem.dao.login.AoaUserDao;
import com.example.oasystem.entity.page.Page;
import com.example.oasystem.entity.user.AoaUser;
import com.example.oasystem.service.AoaUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
public class AttendMonthController {
    String sousuo =null;
    String oldMonth;
    @Autowired
    private AoaUserService aoaUserService;
    @Autowired
    private AoaUserDao aoaUserDao;
    @RequestMapping("/attendcemonth")
    public String toMonthTable(Model model){
        Page page = new Page(1,0,0,0,false,false);
        model.addAttribute("page",page);
        model.addAttribute("url","searchrealmonthtable");
        return "attendce/monthtable";
    }

    @RequestMapping("/realmonthtable")
    public String realmonthtable(Model model, HttpSession session,
                                 @RequestParam(value = "month",required = false,defaultValue = "") String month,
                                 @RequestParam(value = "baseKey",required = false,defaultValue = "") String baseKey){

        AoaUser user = (AoaUser)session.getAttribute("user");

        List<AoaUser> aoaUsers = aoaUserService.selectUserDeptAndAttendsListByMonth(user.getUserId(),month,baseKey,0,0);
        int totalElement= aoaUsers.size();
        int size=3;
        int totalPages=totalElement%size==0? totalElement/size:totalElement/size+1;
        int number=0;
        boolean last;
        if (number==(totalPages-1) || totalPages == 0) {
            last=true;
        } else {
            last=false;
        }
        Page page=new Page(totalElement,size,totalPages,number, true,last);
        //List<AoaUser> ulist = aoaUserService.selectUserDeptAndAttendsListByMonth(user.getUserId(),month,baseKey,0,size);
        List<AoaUser> ulist = aoaUserDao.selectAllXiashu(user.getUserId());
        sousuo = baseKey;
        oldMonth= month;
        HashMap uMap = aoaUserService.getUMap(user.getUserId(), month);

        List<Integer> daysList = new ArrayList<>();
        daysList.add(0);
        daysList.add(0);
        daysList.add(0);
        daysList.add(0);
        daysList.add(0);
        daysList.add(0);
        if(uMap.isEmpty()){
            for (AoaUser each : ulist) {
                uMap.put(each.getUserName(),daysList);
            }
        }
        model.addAttribute("uMap",uMap);
        model.addAttribute("ulist",ulist);

        model.addAttribute("baseKey",baseKey);
        model.addAttribute("page",page);
        model.addAttribute("url","searchrealmonthtable");
        return "attendce/realmonthtable";
    }

    @RequestMapping("/searchrealmonthtable")
    public String searchrealmonthtable(Model model, HttpSession session,
                                       @RequestParam(value = "page",required = false,defaultValue = "0") int page,
                                       @RequestParam(value = "month",required = false,defaultValue = "") String month,
                                       @RequestParam(value = "baseKey",required = false,defaultValue = "") String baseKey){
        AoaUser user = (AoaUser)session.getAttribute("user");
        if(month.equals("")){
            month = oldMonth;
        }
        if(baseKey.equals("")){
            baseKey = sousuo;
        }
        List<AoaUser> aoaUsers = aoaUserService.selectUserDeptAndAttendsListByMonth(user.getUserId(), month, baseKey, 0, 0);

        int totalElement= aoaUsers.size();
        int size = 3;
        int totalPages=totalElement%size==0? totalElement/size:totalElement/size+1;
        int number=page;
        boolean first;
        if (number==0) {
            first=true;
        } else {
            first=false;
        }
        boolean last;
        if (number==(totalPages-1) || totalPages == 0) {
            last=true;
        } else {
            last=false;
        }
        Page fenye = new Page(totalElement,size,totalPages,number, first,last);
        //List<AoaUser> ulist = aoaUserService.selectUserDeptAndAttendsListByMonth(user.getUserId(),month,baseKey,number*size,size);
        List<AoaUser> ulist = aoaUserDao.selectAllXiashu(user.getUserId());
        HashMap uMap = aoaUserService.getUMap(user.getUserId(), month);
        List<Integer> daysList = new ArrayList<>();
        daysList.add(0);
        daysList.add(0);
        daysList.add(0);
        daysList.add(0);
        daysList.add(0);
        daysList.add(0);
        if(uMap.isEmpty()){
            for (AoaUser each : ulist) {
                uMap.put(each.getUserName(),daysList);
            }
        }
        model.addAttribute("uMap",uMap);
        model.addAttribute("ulist",ulist);
        model.addAttribute("baseKey",baseKey);
        model.addAttribute("page",fenye);
        model.addAttribute("url","searchrealmonthtable");
        return "attendce/realmonthtable";
    }
}

